package com.example.netchat;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class RoomListAdapter extends SimpleCursorAdapter {

	private static final String[] SOURCE_COLUMNS = new String[] { RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_NAME_COLUMN_INDEX] };
	private static final int[] DESTINATION_VIEWS = new int[] { android.R.id.text1 };
	private RoomListActionListener mListener;

	public RoomListAdapter(Context context, Cursor cursor,
			RoomListActionListener listener) {
		super(context, R.layout.listitem_room, cursor, SOURCE_COLUMNS,
				DESTINATION_VIEWS,
				SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

		mListener = listener;
	}

	Boolean processing = false;
	final Object locker = new Object();

	@Override
	public void bindView(View view, Context context, final Cursor cursor) {
		super.bindView(view, context, cursor);

		((TextView) view.findViewById(android.R.id.text1)).setText(cursor
				.getString(RoomsContentProvider.ROOM_NAME_COLUMN_INDEX));

		final long id = cursor.getLong(RoomsContentProvider.ID_COLUMN_INDEX);
		final String roomId = cursor
				.getString(RoomsContentProvider.ROOM_ID_COLUMN_INDEX);

		view.findViewById(R.id.chat_room_action).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mListener.onEnterRoomAction(roomId);
					}
				});
		view.findViewById(R.id.delete_room_action).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mListener.onRoomDelete(id);
					}
				});
		view.findViewById(R.id.share_room_action).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mListener.onRoomShare(id);
					}
				});

		final ToggleButton star = (ToggleButton) view.findViewById(R.id.star);

		star.setOnCheckedChangeListener(null);
		try {
			star.setChecked(cursor
					.getLong(RoomsContentProvider.FAVORITE_COLUMN_INDEX) == 1);
		} catch (Exception e) {
			e.printStackTrace();
		}

		star.setTag(id);

		star.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (processing) {
					return;
				}
				synchronized (locker) {
					processing = true;
					star.setOnCheckedChangeListener(null);
					mListener.onRoomToggleFavoriteAction(
							((Long) buttonView.getTag()).longValue(),
							!isChecked);
					star.setOnCheckedChangeListener(this);
					processing = false;
				}
			}
		});
	}

	@Override
	public View newView(Context context, final Cursor cursor, ViewGroup parent) {
		return LayoutInflater.from(mContext).inflate(R.layout.listitem_room,
				null);
	}

}
